package cn.zjutleo.village_server.controller;

import cn.zjutleo.village_server.dto.ID;
import cn.zjutleo.village_server.dto.PolicyDto;
import cn.zjutleo.village_server.dto.PolicySelectDto;
import cn.zjutleo.village_server.exception.apiException.daoException.SelectException;
import cn.zjutleo.village_server.service.PolicyService;
import cn.zjutleo.village_server.vo.PolicyVo;
import cn.zjutleo.village_server.vo.ResponseBean;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "政策管理模块")//修饰整个类，描述Controller的作用 对应swagger大的模块标题
@RestController
@RequestMapping("/api/policy")
public class PolicyController {
    @Resource
    private PolicyService policyService;

    @ApiOperation("添加政策")
    @PostMapping("/add")
    private ResponseBean<String> addPolicy(@RequestBody PolicyDto policyDto) {
        PolicyVo policyVo = new PolicyVo();
        BeanUtils.copyProperties(policyDto, policyVo);
        policyService.savePolicy(policyVo);
        return new ResponseBean<>("success");
    }

    @ApiOperation("更新政策")
    @PostMapping("/update")
    private ResponseBean<String> updatePolicy(@RequestBody PolicyDto policyDto) {
        PolicyVo policyVo = new PolicyVo();
        BeanUtils.copyProperties(policyDto, policyVo);
        policyService.updatePolicyById(policyDto);
        return new ResponseBean<>("success");
    }

    @ApiOperation("根据id获取政策信息")
    @PostMapping("/get")
    private ResponseBean<PolicyDto> getPolicy(@RequestBody ID id) {
        PolicyDto policyDto = new PolicyDto();
        try {
            PolicyVo policyVo = policyService.getExistPolicyVoById(id.getId());
            BeanUtils.copyProperties(policyVo, policyDto);
        } catch (SelectException e) {
            e.printStackTrace();
            return new ResponseBean(200, e.getMessage(), null);
        }
        return new ResponseBean<>(policyDto);
    }

    @ApiOperation("删除政策")
    @PostMapping("/delete")
    private ResponseBean<String> deletePolicy(@RequestBody ID id) {
        policyService.deletePolicyById(id.getId());
        return new ResponseBean<>("success");
    }

    /**
     * 政策列表查询,不知道谁写的,注释后来加上
     * @author zk
     * @date 2022/5/7 8:58
 	 * @param policySelectDto
	 * @return cn.zjutleo.village_server.vo.ResponseBean<java.util.List<cn.zjutleo.village_server.dto.PolicyDto>>
     */
    @PostMapping("/page")
    private ResponseBean<List<PolicyDto>> selectPolicyList(@RequestBody PolicySelectDto policySelectDto) {
        List<PolicyDto> result = policyService.selectPolicyList(policySelectDto);
        return new ResponseBean<>(result);
    }

    /**
     * 政策分页查询2,不知道谁写的,注释后来加上
     * @author zk
     * @date 2022/5/7 8:58
 	 * @param policySelectDto
 	 * @param start 起始页
 	 * @param limit 结束页
	 * @return cn.zjutleo.village_server.vo.ResponseBean<com.github.pagehelper.PageInfo<cn.zjutleo.village_server.dto.PolicyDto>>
     */
    @PostMapping("/pageList/{start}/{limit}")
    private ResponseBean<PageInfo<PolicyDto>> selectPolicyPage(@RequestBody PolicySelectDto policySelectDto, @PathVariable("start") Integer start, @PathVariable("limit") Integer limit) {
        PageInfo<PolicyDto> result = policyService.selectPolicyPage(policySelectDto,start,limit);
        return new ResponseBean<>(result);
    }


}
